#include <Windows.h>
#define szAppName "DEMO"
HINSTANCE m_hInstance;// pointer contain instance of program
HWND m_hWND;// pointer contain window

/*
 * Prototype of program
 */
bool InitWindow(HINSTANCE hInstance); //Initial window
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Receive and execute message from system and application

/*
 * Prototype of program
 */
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	// Return false if can not initial window
	if (!InitWindow(hInstance))
	{
		return false;
	}

	MSG msg;
	ZeroMemory(&msg, sizeof(msg));

	/*
	* Main loop to manage message
	* Loop until receive a quit message
	*/
	while (msg.message != WM_QUIT)
	{
		// Check for event send to queue
		while (GetMessage(&msg, m_hWND, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (int) msg.wParam;

}

/*
 * Create window has 3 basic step:
 * + Register window
 * + Create window
 * + Show window
 * param hInstance pointer contain instance of program
 */
bool InitWindow(HINSTANCE hInstance)
{
	// Init value and register window
	WNDCLASS wndClass;
	wndClass.style = CS_HREDRAW | CS_VREDRAW; // style of class
	wndClass.lpfnWndProc = (WNDPROC)WndProc; // inform callback to window procedure
	wndClass.hInstance = m_hInstance; // pointer handle point to instance of application

	wndClass.cbClsExtra = 0; // byte allocate extra to class
	wndClass.cbWndExtra = 0; // byte allocate extra to every window of application
	wndClass.hCursor = LoadCursor(hInstance, IDC_ARROW); // Cursor of program
	wndClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION); // Icon of program
	wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Fill background colour for window
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = szAppName;

	RegisterClass(&wndClass); // REgister for class window

	// Create window
	m_hWND = CreateWindow(szAppName, "Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, m_hInstance, NULL);

	if (!m_hWND)
	{
		return false;
	}

	//Show window
	ShowWindow(m_hWND, SW_MAXIMIZE);
	UpdateWindow(m_hWND);
	return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_PAINT:
		{
			// we would place our Windows painting code here.
			HDC hdc;
			PAINTSTRUCT ps;
			hdc = BeginPaint( hWnd, &ps );

			// draw a circle and a 2 squares
			Ellipse( hdc, 20, 20, 160, 160 );
			Rectangle( hdc, 50, 50, 90, 90 );
			Rectangle( hdc, 200, 50, 140, 90 );

			EndPaint( hWnd, &ps );
		}
		return 0;
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;

	}

	return DefWindowProc(hWnd,message,wParam,lParam);
}